package com.bw.merchant.page

import android.os.Bundle
import android.widget.SearchView.OnQueryTextListener
import androidx.activity.viewModels
import androidx.appcompat.app.AppCompatActivity
import androidx.appcompat.widget.SearchView
import androidx.core.view.ViewCompat
import androidx.core.view.WindowInsetsCompat
import com.alibaba.android.arouter.facade.annotation.Route
import com.alibaba.android.arouter.launcher.ARouter
import com.blankj.utilcode.util.ToastUtils
import com.bw.base.path.RouterPath
import com.bw.common.view.BaseMvvmActivity
import com.bw.merchant.R
import com.bw.merchant.adapter.MerchantAdapter
import com.bw.merchant.databinding.ActivityMerchantBinding
import com.bw.merchant.entity.MerchantListEntityItem
import com.bw.merchant.vm.MerchantViewModel
import dagger.hilt.android.AndroidEntryPoint

/**
 * 企业列表
 */

@Route(path = RouterPath.PATH_MERCHANT_PATH)
@AndroidEntryPoint
class MerchantActivity : BaseMvvmActivity<ActivityMerchantBinding>() {


    override fun getLayoutId(): Int =R.layout.activity_merchant
    private val mViewModel:MerchantViewModel by viewModels()
    val mAdapter = MerchantAdapter()

    var list = listOf<MerchantListEntityItem>()
    override fun initViews() {
        super.initViews()
        //点击返回
        mDataBinding.back.setOnClickListener {
            finish()
        }
        //点击跳转到入驻申请
        mDataBinding.addMerchant.setOnClickListener {
            ARouter.getInstance().build(RouterPath.PATH_SETTLED_PATH).navigation()
        }
        //搜索
            mDataBinding.merchantSearch.setOnQueryTextListener(object : OnQueryTextListener {
                override fun onQueryTextSubmit(p0: String?): Boolean {
                    if (p0!=null){
                        mAdapter.data.clear()
                        mAdapter.data.addAll(list.filter { it.name.contains(p0) })
                        mAdapter.notifyDataSetChanged()
                    }
                        return  false
                }

                override fun onQueryTextChange(p0: String?): Boolean {
                    mViewModel.getMerchantData()
                    return false
                }
            })



        mDataBinding.rvMerchant.adapter = mAdapter
        mViewModel.mMerchantSuccess.observe(this){
            list = it
            mAdapter.data.clear()
            mAdapter.data.addAll(it)
            mAdapter.notifyDataSetChanged()
        }


        //点击跳转到详情页
        mAdapter.setOnItemClickListener { adapter, view, position ->
            ARouter.getInstance().build(RouterPath.PATH_MERCHANT_DETAIL_PATH)
                .withString("aid",mAdapter.data[position].id.toString())
                .navigation()
        }
    }



    override fun onResume() {
        super.onResume()
        mViewModel.getMerchantData()
    }
}